package com.cysion.study.widget.pay;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import com.alipay.sdk.app.PayTask;

import java.util.Map;

/**
 * Created by chu on 2019-07-18.
 */
public class PaymentHelper {
    private static final int SDK_PAY_FLAG = 1;
    private Activity mthis;
    private Map<String, String> result;
    private Activity activity;
    private PayCallBack mCallBack;



    public PaymentHelper(Activity activity, PayCallBack mCallBack) {
        super();
        this.activity = activity;
        this.mCallBack = mCallBack;
    }


    /**
     * 支付宝支付
     */
    public void startAliPay(String orderInfo) {
//        this.mthis = activity;
        if (activity == null || orderInfo == null) {
            return;
        }

        new AliPayThread(orderInfo).start();  //支付行为需要在独立的非ui线程中执行
    }

    /**
     * 支付宝支付异步任务
     */
    private class AliPayThread extends Thread {
        private String orderInfo;

        private AliPayThread(String orderInfo) {
            this.orderInfo = orderInfo;
        }

        @Override
        public void run() {
            PayTask alipay = new PayTask(activity);
            result = alipay.payV2(orderInfo, true);
            Log.i("支付宝订单结果=", "result=" + result.toString());
            Message msg = new Message();
            msg.what = SDK_PAY_FLAG;
            msg.obj = result;
            /**
             *  官方result返回结果参考：https://docs.open.alipay.com/204/105302
             *  我这里返回到result格式为：
             *      {
             resultStatus = 9000, result = {
             "alipay_trade_app_pay_response": {
             "code": "10000",
             "msg": "Success",
             "app_id": "2017112400138529",
             "auth_app_id": "2017112400138529",
             "charset": "utf-8",
             "timestamp": "2018-01-29 14:46:33",
             "total_amount": "0.01",
             "trade_no": "2018012921001004940219217398",
             "seller_id": "2088821472668202",
             "out_trade_no": "0129144616-2725"
             }
             */
            Log.d("zjp", "result=" + result);
            mHandler.sendMessage(msg);
        }
    }

    @SuppressLint("HandlerLeak")
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case SDK_PAY_FLAG:
                    // 支付宝返回此次支付结果及加签，建议对支付宝签名信息拿签约时支付宝提供的公钥做验签
                    Map<String, String> mapPayResult = (Map<String, String>) msg.obj;
                    String resultStatus = mapPayResult.get("resultStatus");
                    // 判断resultStatus 为“9000”则代表支付成功，具体状态码代表参考：https://docs.open.alipay.com/204/105301
                    if (mCallBack != null)
                        mCallBack.complete(resultStatus);
                    /*if (TextUtils.equals(resultStatus, "9000")) {
//                        showToast("支付成功");
                        if (mCallBack != null)
                            mCallBack.complete(resultStatus);
                    } else {
                        if (mCallBack != null) {
                            // 判断resultStatus 为非“9000”则代表可能支付失败
                            // “8000”代表支付结果因为支付渠道原因或者系统原因还在等待支付结果确认，最终交易是否成功以服务端异步通知为准（小概率状态）
                            if (TextUtils.equals(resultStatus, "8000")) {
//                            showToast("");
                                mCallBack.complete("支付结果确认中");
                            } else if (TextUtils.equals(resultStatus, "6001")) { //用户中途取消
//                            showToast("取消支付");
//                                mCallBack.complete("取消支付");
                                mCallBack.complete(resultStatus);
                            } else {
                                // 其他值就可以判断为支付失败
//                            showToast("支付失败");
//                                mCallBack.complete("支付失败");
                                mCallBack.complete(resultStatus);
                            }

                        }
                    }*/
                    break;
            }
        }
    };

    /**
     * 支付回调接口
     *
     * @author admin
     */
    public interface PayCallBack {
        void complete(String info);
    }
}